package com.cui.project.service.impl;

import com.api.common.model.entity.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.api.common.model.entity.InterfaceInfoLog;
import com.cui.project.model.vo.InterfaceInfoLogListVo;
import com.cui.project.service.InterfaceInfoLogService;
import com.cui.project.mapper.InterfaceInfoLogMapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 86176
 * @description 针对表【interface_info_log】的数据库操作Service实现
 * @createDate 2024-10-16 12:48:44
 */
@Service
public class InterfaceInfoLogServiceImpl extends ServiceImpl<InterfaceInfoLogMapper, InterfaceInfoLog>
        implements InterfaceInfoLogService {
    @Override
    public InterfaceInfoLogListVo listInterfaceInfoLogHistoryById(Long interfaceInfoId, User loginUser) {
        InterfaceInfoLogListVo result = new InterfaceInfoLogListVo();

        Long userId = loginUser.getId();
        QueryWrapper<InterfaceInfoLog> interfaceInfoLogQueryWrapper = new QueryWrapper<>();
        interfaceInfoLogQueryWrapper.eq("interface_info_id", interfaceInfoId);
        interfaceInfoLogQueryWrapper.eq("user_id", userId);
        interfaceInfoLogQueryWrapper.select("id", "code", "createTime");
        interfaceInfoLogQueryWrapper.orderByDesc("createTime");
        interfaceInfoLogQueryWrapper.last("limit 10");
        List<InterfaceInfoLog> interfaceInfoLogs = this.list(interfaceInfoLogQueryWrapper);
        List<InterfaceInfoLogListVo.InterfaceInfoCodeVo> interfaceInfoCodeVoList = interfaceInfoLogs.stream().map(item -> {
            InterfaceInfoLogListVo.InterfaceInfoCodeVo interfaceInfoCodeVo = new InterfaceInfoLogListVo.InterfaceInfoCodeVo();
            interfaceInfoCodeVo.setCode(item.getCode());
            interfaceInfoCodeVo.setLastTime(item.getCreateTime());
            return interfaceInfoCodeVo;
        }).collect(Collectors.toList());
        result.setInterfaceInfoCodeVos(interfaceInfoCodeVoList);

        /**
         * 这边是获取正确与故障的次数，正确：200. 故障：500
         */
        Map<String, Object> historySuccessAndFailCount = this.baseMapper.selectHistorySuccessAndFailCount(interfaceInfoId);
        if (historySuccessAndFailCount != null) {
            List<InterfaceInfoLogListVo.InterfaceInfoCodeVo> historyInterfaceInfoLogListVos = new ArrayList<>();
            Object success = historySuccessAndFailCount.get("success");
            if (success != null) {
                Date lastSuccessTime = localDateTimeToDate(historySuccessAndFailCount.get("lastSuccessTime"));
                InterfaceInfoLogListVo.InterfaceInfoCodeVo successInterfaceInfoCodeVo = new InterfaceInfoLogListVo.InterfaceInfoCodeVo();
                successInterfaceInfoCodeVo.setAccNum(((BigDecimal) success).longValue());
                successInterfaceInfoCodeVo.setCode(200);
                successInterfaceInfoCodeVo.setLastTime(lastSuccessTime);
                historyInterfaceInfoLogListVos.add(successInterfaceInfoCodeVo);
            }
            Object fail = historySuccessAndFailCount.get("fail");
            if (fail != null) {
                InterfaceInfoLogListVo.InterfaceInfoCodeVo faillInterfaceInfoCodeVo = new InterfaceInfoLogListVo.InterfaceInfoCodeVo();
                Date lastSuccessTime = localDateTimeToDate(historySuccessAndFailCount.get("lastSuccessTime"));
                faillInterfaceInfoCodeVo.setCode(500);
                faillInterfaceInfoCodeVo.setAccNum(((BigDecimal) fail).longValue());
                faillInterfaceInfoCodeVo.setLastTime(lastSuccessTime);
                historyInterfaceInfoLogListVos.add(faillInterfaceInfoCodeVo);
            }
            result.setInterfaceInfoAnaylsisVos(historyInterfaceInfoLogListVos);
        }

        return result;
    }

    private Date localDateTimeToDate(Object date) {
        LocalDateTime localDateTime = (LocalDateTime) date;
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.format(Date.from(zonedDateTime.toInstant()));
//        DateU
        return Date.from(zonedDateTime.toInstant()) ;
    }
}




